function getNotification()
{
	var xmlHttp = createXmlHttpRequestObject();
	var url = "./getNotification.php"
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.onreadystatechange = function(){afficherInfo(xmlHttp)};
	xmlHttp.send(null);
}

function getCommentaire(id)
{
	var xmlHttp = createXmlHttpRequestObject();
	var url = "./getCommentaire.php?id=" + id;
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.onreadystatechange = function(){afficherCommentaire(xmlHttp)};
	xmlHttp.send(null);
}

function afficherCommentaire(xmlHttp)
{
	if (xmlHttp.readyState==4 && (xmlHttp.status == 200 || requete.status == 0)) {
		document.body.style.cursor='default';
		var donnees = JSON.parse(xmlHttp.responseText);
		alert('Le commentaire a ete ecrit par "' + donnees.auteur + '" le "' + donnees.date + ' pour l\'information "' + donnees.titre + '": \n\n' + donnees.contenu);
	}else if (xmlHttp.readyState < 4) {
		document.body.style.cursor='wait';
	}
}

function visualiser(e){
	var donnees = e.target.id.split('_');
	if(donnees[1] == 'inf'){
		window.location = "./ajout-maj_information.php?choix=update&id=" +donnees[2];	
	}else if(donnees[1] == 'com'){
		getCommentaire(donnees[2]);
	}else{
		var identifiant = donnees[3].split(' ');
		window.location = "./modifier_compte_adherent.php?prenom=" + identifiant[0] + "&nom=" + identifiant[1];
	}
	
}

function rechargerInformation(xmlHttp){
	if (xmlHttp.readyState==4 && (xmlHttp.status == 200 || requete.status == 0)) {
		document.body.style.cursor='default';
		var div = document.getElementById('table');
		var longeur = div.childNodes.length;
		for(var i=0;i<longeur;i++){
			div.removeChild(div.firstChild);
		}
		getNotification();
	}else if (xmlHttp.readyState < 4) {
		document.body.style.cursor='wait';
	}
}

function acc_ref_Notif(e){
	var xmlHttp = createXmlHttpRequestObject();
	var donnees = e.target.id.split('_');
	xmlHttp.open("POST", "./acc_ref_Notif.php?op=" + donnees[0] + "&type=" + donnees[1] + "&id=" + donnees[2], true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.onreadystatechange = function(){rechargerInformation(xmlHttp)};
	xmlHttp.send(null);
}

function afficherInfo(xmlHttp)
{
	if (xmlHttp.readyState==4 && (xmlHttp.status == 200 || requete.status == 0)) {
		// on vide la table
		var div = document.getElementById('table');
		document.body.style.cursor='default';
		var notifications = JSON.parse(xmlHttp.responseText);
		var tab,row,cell_date,cell_type,cell_auteur,cell_visualiser,cell_acc,cell_ref;
		var bouton_visualiser,bouton_acc,bouton_ref;
		
		if(notifications.length == 0){
			div.appendChild(document.createTextNode("Aucune notification"));
		}else{
			// on créé le tableau des notifications
			row = new Array();
			tab=document.createElement('table');
			tab.setAttribute('id','table_notif');
			row[0]=document.createElement('tr');
			row[0].setAttribute('id','premiere_ligne');
			cell_date = document.createElement('td');
			cell_date.appendChild(document.createTextNode("Date"));
			cell_type = document.createElement('td');
			cell_type.appendChild(document.createTextNode("Type"));
			cell_auteur = document.createElement('td');
			cell_auteur.appendChild(document.createTextNode("Auteur"));
			cell_visualiser = document.createElement('td');
			cell_visualiser.appendChild(document.createTextNode("Visualiser"));
			cell_acc = document.createElement('td');
			cell_acc.appendChild(document.createTextNode("Accepter"));
			cell_ref = document.createElement('td');
			cell_ref.appendChild(document.createTextNode("Refuser"));
			row[0].appendChild(cell_date);
			row[0].appendChild(cell_type);
			row[0].appendChild(cell_auteur);
			row[0].appendChild(cell_visualiser);
			row[0].appendChild(cell_acc);
			row[0].appendChild(cell_ref);
			tab.appendChild(row[0]);
			for(var i=0;i<notifications.length;i++){
				row[i+1] = document.createElement('tr');
				cell_date = document.createElement('td');
				cell_date.appendChild(document.createTextNode(notifications[i].date));
				cell_type = document.createElement('td');
				cell_type.appendChild(document.createTextNode(notifications[i].type));
				cell_auteur = document.createElement('td');
				cell_auteur.appendChild(document.createTextNode(notifications[i].auteur));
				
				cell_visualiser = document.createElement('td');
				bouton_visualiser = document.createElement('button');
				bouton_visualiser.setAttribute('id',"v_" + notifications[i].type.substr(0,3) + "_" + notifications[i].id + "_" + notifications[i].auteur);
				bouton_visualiser.setAttribute('class','visualiser');
				bouton_visualiser.addEventListener("click",visualiser);
				
				cell_acc = document.createElement('td');
				bouton_acc = document.createElement('button');
				bouton_acc.setAttribute('id',"a_" + notifications[i].type.substr(0,3) + "_" + notifications[i].id);
				bouton_acc.setAttribute('class','accepter');
				bouton_acc.addEventListener("click",acc_ref_Notif);
				
				cell_ref = document.createElement('td');
				bouton_ref = document.createElement('button');
				bouton_ref.setAttribute('id',"r_" + notifications[i].type.substr(0,3) + "_" + notifications[i].id);
				bouton_ref.setAttribute('class','refuser');
				bouton_ref.addEventListener("click",acc_ref_Notif);
		
				cell_visualiser.appendChild(bouton_visualiser);
				cell_acc.appendChild(bouton_acc);
				cell_ref.appendChild(bouton_ref);
		
				row[i+1].appendChild(cell_date);
				row[i+1].appendChild(cell_type);
				row[i+1].appendChild(cell_auteur);
				row[i+1].appendChild(cell_visualiser);
				row[i+1].appendChild(cell_acc);
				row[i+1].appendChild(cell_ref);
				tab.appendChild(row[i+1]);
			}
			
			document.getElementById('table').appendChild(tab);
		}
	}else if (xmlHttp.readyState < 4) {
		document.body.style.cursor='wait';
	}
}

addListener(window,"load",getNotification);
